package Servlet.member;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/LogOff")
public class LogOffServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取session对象
        HttpSession session = req.getSession();
        // 强制清除session
        session.invalidate();

        // 将sessionId放入到Cookie里
        Cookie cookie = new Cookie("JSESSIONID",session.getId());
        // 清除Cookie
        cookie.setMaxAge(0);
        // 添加Cookie
        resp.addCookie(cookie);

        // 重定向
//        resp.sendRedirect("/javaweb/index.jsp");
        req.getRequestDispatcher("/login").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
